//
//  SetUserInfoVC.m
//  CareRing
//
//  Created  on 2024/12/14.
//
    

#import "SetUserInfoVC.h"
#import "UIButton+demoStyle.h"
#import "CARingSDK.h"
#import "NSObject+Tool.h"
#import "CABLeService.h"
#import "UIViewController+Custom.h"
#import "ConfigModel.h"

@interface SetUserInfoVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *genderLbl;
@property (weak, nonatomic) IBOutlet UILabel *ageLbl;
@property (weak, nonatomic) IBOutlet UILabel *Lweightbl;
@property (weak, nonatomic) IBOutlet UILabel *heightLbl;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segGender;
@property (weak, nonatomic) IBOutlet UITextField *ageTf;
@property (weak, nonatomic) IBOutlet UITextField *heightTf;
@property (weak, nonatomic) IBOutlet UITextField *weightTf;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;

@end

@implementation SetUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}

-(void)initUI
{
    [self.sureBtn functionStyle];
    [self.cancleBtn functionStyle];
    
    self.genderLbl.text = _L2(L_UD_SEX);
    self.ageLbl.text = _L2(L_UD_AGE);
    self.Lweightbl.text = _L2(L_UD_WEIGHT);
    self.heightLbl.text = _L2(L_UD_HEIGHT);
    
    self.ageTf.delegate = self;
    self.weightTf.delegate = self;
    self.heightTf.delegate = self;

    
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
    if (textField == self.ageTf) {
        // Age: range 1-115
        NSUInteger age = (NSUInteger)[self.ageTf.text intValue];
        if (age < 1 || age > 115) {
            [self showAlertWarningWithTitle:@"Error" Msg:@"Age: range in 1-115." btnCancel:_L2(L_CANCEL) Compelete:nil];
        }
        return;
    }
    
    if (textField == self.weightTf) {
        // Weight: in kg, range 30-200
        NSUInteger weight = (NSUInteger)[self.weightTf.text intValue];
        if (weight < 30 || weight > 200) {
            [self showAlertWarningWithTitle:@"Error" Msg:@"Weight: in kg, range 30-200." btnCancel:_L2(L_CANCEL) Compelete:nil];
        }
        return;
    }
    
    if (textField == self.heightTf) {
        // Height: in mm, range 1200-3000
        NSUInteger height = (NSUInteger)[self.heightTf.text intValue];
        if (height < 1200 || height > 3000) {
            [self showAlertWarningWithTitle:@"Error" Msg:@"Height: in mm, range 1200-3000." btnCancel:_L2(L_CANCEL) Compelete:nil];
        }
        return;
    }
    
}

- (IBAction)sureClick:(id)sender {
    NSUInteger gender = self.segGender.selectedSegmentIndex;
    NSUInteger age = (NSUInteger)[self.ageTf.text intValue];
    NSUInteger height = (NSUInteger)[self.heightTf.text intValue];
    NSUInteger weight = (NSUInteger)[self.weightTf.text intValue];
    
    if (CARingSDK.instance.currentDevice.isConnected) {
        
        [CARingSDK.instance functionSetUsrInfoToDev:gender Age:age Height:height Weight:weight];
        
        WEAK_SELF
        [self mainThreadDelayAfter:0.5 Blk:^{
            STRONG_SELF
            [strongSelf dismissViewControllerAnimated:YES completion:nil];
        }];
        
    } else {
        [self showAlertWarningWithTitle:@"Tips" Msg:@"Ble disconnected! Set failed." btnCancel:_L2(L_CANCEL) Compelete:nil];
    }
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];

}
- (IBAction)cancelClick:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];
    
  
   
}

@end
